Skip to content

Fix panic on text align with unicode#404

Merged
jedib0t merged 1 commit intojedib0t:mainfrom
edznux-dd:fix/justify-text-cjk-panic
Apr 21, 2026
Merged

Fix panic on text align with unicode#404
jedib0t merged 1 commit intojedib0t:mainfrom
edznux-dd:fix/justify-text-cjk-panic

Conversation

@edznux-dd
Copy link
Copy Markdown
Contributor

Proposed Changes

Hey, small PR after doing some cleanup :)

It's possible to panic on text.justifyText with strings: negative Repeat count when a cell's display width (CJK-aware, via go-runewidth) exceeds maxLength.
The source of the issue is that numSpacesNeeded = maxLength - textLength + spaces goes negative, then is passed to strings.Repeat, which does panic on negative see here)

I've made this fix: early-return the text unchanged when numSpacesNeeded < 0 (consistent with how AlignCenter handles the overflow case), as it seemed the best way to fix.

If you want a small and easy reproducer that you can run:

package main

import "github.com/jedib0t/go-pretty/v6/text"

func main() {
      // "䈢" (U+4222) has display width 2, so the full string's display width is 12, which exceeds maxLength=10 and triggers:
      //   panic: strings: negative Repeat count
      text.AlignJustify.Apply("a 䈢䈢䈢䈢䈢", 10)
}

I also added a small fuzz test: it won't really run as a fuzz test in your CI as far as I can tell, but should allow you to find similar issues in the future if you run it manually. (Doing the CI setup change to run go test -fuzz is outside of the scope of this PR haha)

@sonarqubecloud
Copy link
Copy Markdown

@coveralls
Copy link
Copy Markdown

Coverage Report for CI Build 24737841796

Coverage remained the same at 100.0%

Details

  • Coverage remained the same as the base build.
  • Patch coverage: 7 of 7 lines across 1 file are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 4881
Covered Lines: 4881
Line Coverage: 100.0%
Coverage Strength: 1.21 hits per line

💛 - Coveralls

@jedib0t
Copy link
Copy Markdown
Owner

jedib0t commented Apr 21, 2026

Awesome! Thanks! 🙏

@jedib0t jedib0t merged commit 66563fd into jedib0t:main Apr 21, 2026
4 checks passed
@jedib0t
Copy link
Copy Markdown
Owner

jedib0t commented Apr 22, 2026

@edznux-dd Here is a tag you can use: https://github.com/jedib0t/go-pretty/releases/tag/v6.7.10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants